package com.opensky.qatest.util.gmail;

import org.openqa.selenium.By;

import com.opensky.qatest.util.selenium.WebDriverWrapper;

public class GmailManager {

    private static final String USER_FIELD = "//input[@id='Email' and @type='text']";

    private static final String PASSWD_FIELD = "//input[@id='Passwd']";

    private static final String SIGNIN_BTN = "signIn";

    private static final String GMAIL_URL = "http://www.gmail.com/";

    public static final String GMAIL_USER = "qatest.opensky@gmail.com";

    public static final String GMAIL_PASSWD = "Opensky2011";

    public static final String MESSAGE_INFO_LINE = "//tbody/tr[@id=':pb']";

    public static final String MAIN_FRAME = "canvas_frame";

    public static final String MAIN_DROPDOWN = "gbgs4";

    public static final String SIGNOUT_LINK = "gb_71";

    public static final String SELECT_ALL_CHECKBOX = "input.J-Pm-Jo";

    public static final String DELETE_BTN = "//div[@role='button' and @act='10']";

    public static final String TOPIC = "//div[@class='y6']//b";

    public static final String INBOX = "//a[contains(., 'Inbox ')]";

    public static final long TIMEOUT = 10;

    private final WebDriverWrapper driver;

    private String returnURL;

    public GmailManager(WebDriverWrapper driver) {
	this.driver = driver;
	returnURL = driver.getCurrentUrl();
    }

    private void signIn() {
	driver.pause(TIMEOUT);
	driver.get(GMAIL_URL);
	if (driver.findElements(By.id(SIGNIN_BTN)).size() != 0) {
	    if (driver.findElements(By.xpath(USER_FIELD)).size() != 0) {
		driver.waitAndSendKeys("USER_FIELD", By.xpath(USER_FIELD), GMAIL_USER);
	    }
	    driver.waitAndSendKeys("PASSWD_FIELD", By.xpath(PASSWD_FIELD), GMAIL_PASSWD);
	    driver.waitAndClick("SIGNIN_BTN", By.id(SIGNIN_BTN));
	}
    }

    private void signOut() {
	focusMainFrame();
	driver.pause(TIMEOUT);
	driver.waitAndClick("MAIN_DROPDOWN", By.id(MAIN_DROPDOWN));
	driver.waitAndClick("SIGNOUT_LINK", By.id(SIGNOUT_LINK));
	driver.sielentAlert();
    }

    public void removeMessages() {
	signIn();
	driver.waitAndVerifyElementPresent("MAIN_FRAME", By.id(MAIN_FRAME));
	focusMainFrame();
	driver.waitAndClick("SELECT_ALL_CHECKBOX", By.cssSelector(SELECT_ALL_CHECKBOX));
	driver.waitAndClick("DELETE_BTN", By.xpath(DELETE_BTN));
	signOut();

	driver.get(returnURL);
    }

    private void focusMainFrame() {
	driver.switchTo().defaultContent();
	driver.switchTo().frame(MAIN_FRAME);
    }

    public boolean isMessageReceived(String topic, long timeout) {
	signIn();
	driver.waitAndVerifyElementPresent("MAIN_FRAME", By.id(MAIN_FRAME));
	focusMainFrame();

	if (!driver.waitTimeAndVerifyElementPresent("TOPIC", By.xpath(TOPIC), timeout)) {
	    driver.waitAndClick("INBOX", By.xpath(INBOX));
	    driver.waitAndVerifyElementPresent("TOPIC", By.xpath(TOPIC));
	}

	boolean result = driver.findElement(By.xpath(TOPIC)).getText().equals(topic);

	driver.get(returnURL);

	return result;
    }

    public static String getGmailUser() {
	return GMAIL_USER;
    }
}
